package cn.sdnu.database.main;

import cn.sdnu.database.controller.DbController;
import cn.sdnu.database.utils.Utils;
import java.util.Scanner;

/**
 * @author Master_Joe qiaoguangtong@aliyun.com
 */
public class Main {
	private static StringBuilder sql;
	private static String input;
	private static boolean isIndent;
	private static String sqlS;
	private static Scanner scanner;

	public static void main(String[] args) {
		scanner = new Scanner(System.in);
		while (true) {
			// for every time we enter an sql statement, we will enter sql statement behind
			// qfsql>>
			sql = new StringBuilder();
			input = "";
			System.out.print("qfsql");
			isIndent = false;
			do {
				if (isIndent) {
					System.out.printf("%5s", " ");
				}
				isIndent = true;
				System.out.print(">> ");
				input = scanner.nextLine() + " ";
				if (input.endsWith("exit; ") || input.endsWith("exit ")) {
					System.out.println("Bye");
					System.exit(0);
				}
				sql.append(input);
			} while (!input.endsWith(";") && !input.contains(";"));
			sqlS = sql.toString();
			if (!Utils.matchBracket(sqlS, '(') || !Utils.matchBracket(sqlS, '[') || !Utils.matchBracket(sqlS, '{')) {
				System.out.println("You have some syntax error, please try again");
				continue;
			}
			DbController.getInstance().sqlHandle(sqlS);
		}
	}
}
